import java.applet.Applet;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;


public class SOCApplet extends Applet implements Runnable, MouseListener{
	
	Thread runner;
	TileHolder tileHolder;
	public static final int SIZE = 50;
	MenuHolder menus;
	ArrayList<Player> players = new ArrayList<Player>();
	ArrayList<Clickable> clickable;
	Turn turn;
	Thief thief;
	
	public void init() {
		setSize(SIZE * 16, SIZE * 14);
		setBackground(Color.BLUE);
	
		// Start animation
		runner = new Thread(this);
		runner.start();
		
		// Respond to mouse actions
		addMouseListener(this);
		
		clickable = new ArrayList<Clickable>();
		
		tileHolder = new TileHolder(SIZE, this);
		
		for(int i = 0; i < 4; i ++){
			Player p = new Player(i, this);
			players.add(i, p);
		}
		players.get(0).setColor(Color.RED);
		players.get(1).setColor(Color.BLUE);
		players.get(2).setColor(Color.GREEN);
		players.get(3).setColor(Color.BLACK);
		
		//sets up the menus
		turn = new Turn(players.get(0));
		menus = new MenuHolder(SIZE, players, this);
		
		//creates thief
		thief = new Thief(this);
	}	

	public ArrayList<Player> getPlayers() {
		return players;
	}

	public void setPlayers(ArrayList<Player> players) {
		this.players = players;
	}

	public Thief getThief() {
		return thief;
	}

	public void setThief(Thief thief) {
		this.thief = thief;
	}

	public void setTurn(Turn turn) {
		this.turn = turn;
	}

	public void paint(Graphics g) {
		//draws all the tiles
		tileHolder.drawTiles(g);
		for(Player p : players){
			if (!turn.getPlayer().equals(p))
				p.getPlayerView().drawSelf(g);
			ArrayList<Building> buildings = p.getBuildings();
			for(Building b : buildings){
				b.drawSelf(g);
			}
		}
		thief.drawSelf(g);
		menus.drawSelf(g);
	}
	
	public void addToClickable(Clickable c){
		clickable.add(c);
	}
	
	/* Run is completed in parallel to other operations in the class
	 * In this case, repaint is called over and over again to create animation
	 */
	@Override
	public void run() {
		while(true) {
			repaint();
			try {
				Thread.sleep(1000); // pause between frames
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public boolean nextTurn(){
		int temp = players.lastIndexOf(turn.getPlayer());
		temp++;
		turn.getPlayer().findLongestRoad();
		if (temp == players.size())
			temp = 0;
		turn = new Turn(players.get(temp));
		for (Player p: players){
			p.checkLongest();
			if (p.countVPts() >= 10){
				System.out.println("End game");
			}
		}
		return true;
	}
	
	
	@Override
	public void mouseClicked(MouseEvent e) {
		System.out.println(e.getX() + " " + e.getY());
		ArrayList<Tile> tiles = tileHolder.getTiles();
		ArrayList<Point> points = tileHolder.getPoints();
		ArrayList<sCard> cards = turn.getPlayer().getHand().getCards();
		
		sCard temp = null;
		for(sCard s: cards){
		//	s.overlaps(e.getX(), e.getY());
			if (s.isPlayed()){
//				System.out.println("played");
				temp = s;
			}
		}
		if (temp != null)
			temp.removeSelfFromHand();
		//Implementation of specific object target code - makes testing harder.
		if (turn.getTargetObject() == 'R' || turn.getTargetObject() == 'F'){
//			System.out.println("Road started");
			for (Point p: points){
				if(p.getPointDisplay().overlaps(e.getX(), e.getY())){
					if (turn.getTargetObject() == 'R'){
						System.out.println("Normal road");
						p.startRoad();
					}
					else if (turn.getTargetObject() == 'F') {
						System.out.println("Free road");
						p.startFirstRoad();
					}
				}
			}
		} else if (turn.getTargetObject() == 'P' || turn.getTargetObject() == 'M'){
			for (Point p: points){
				if(p.getPointDisplay().overlaps(e.getX(), e.getY())){
					if (turn.getTargetObject() == 'P')
						p.endRoad();
					else
						p.endFirstRoad();
				}
			}
		} else if (turn.getTargetObject() == 'T'){
			for(Tile t: tiles){
				if(t.getTileDisplay().overlaps(e.getX(), e.getY())){
					thief.move(t);
				}
			}
		}
		
		//TEST CODE, REMOVE LATER
		
		for(Clickable c: clickable){
			c.overlaps(e.getX(), e.getY());
		}
		
		for(Tile t: tiles){
			t.getTileDisplay().overlaps(e.getX(), e.getY());
		}
		for (Point p: points){
			p.getPointDisplay().overlaps(e.getX(), e.getY());
		}
		menus.checkOverlaps(e.getX(), e.getY());
	}
	
	@Override
	public void mouseEntered(MouseEvent arg0) {
		setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public Turn getTurn(){
		return this.turn;
	}
}
